--
-- @Author:      name
-- @DateTime:    2018-08-9 23:05:48
-- @Description: 服务消息的处理

local skynet = require "skynet"
local log = require "Log"
-- local pbc = require "pbc_helper"
-- local config = require "configquery"
local NodeMessage = require "NodeMessage"
local queue = require "skynet.queue"
local msgpack = require "msgpack_helper"
local gate_idx = 1

local Command = class("Command")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message,userObj)
	
	self.msg = message	
	self.userObj = userObj
	self.userAgents = {} --userAgents[fd] = userInfo
	self.nodeMessage = NodeMessage.new()

	self:register()
	skynet.fork(function()
		self:heartLoop()
	end)
end

--注册本服务里的消息
function Command:register()
	self.msg:registerSelf('start',handler(self,self.connect))
	self.msg:registerSelf('init',handler(self,self.initPbc))
	self.msg:registerSelf('disconnect',handler(self,self.disconnect))
	self.msg:registerSelf('notice',handler(self,self.notice))
	self.msg:registerSelf('oper',handler(self,self.oper))
	self.msg:registerSelf('redirect',handler(self,self.redirect))


end

function Command:getUserAgents()
	return self.userAgents
end

function Command:getUserInfo(fd)
	return self.userAgents[fd]
end

function Command:removeUserAgent(fd)
	self.userAgents[fd] = nil
end

--心跳处理
function Command:heartLoop()
	while true do 
		skynet.sleep(100)
		for k,v in pairs(self.userAgents) do 
			v.time = v.time + 1
			if v.time > 30 then --4分钟没收到包断开socket
				print("#######30s没收到包###kick_player#########",v)
				v.time = 0				
				-- skynet.call(gate_manager, 'lua', 'kick_player',v.fd)		
				self.nodeMessage:callService('.proxy', 'kick_player',v.fd)		
			end
		end
	end
end

--
function Command:getMsgpack()
	return msgpack
end

function Command:getUnpackFunc()
	return msgpack.unpack_message
end
---------------------------------------------------------
-- CMD
---------------------------------------------------------


--初始化
function Command:initPbc(pbc_env)
	msgpack.load(pbc_env)	
end

--socket连入
function Command:connect(conf)
	local fd     = conf.fd
	local gate   = conf.gate
	local addr   = conf.addr
	local contype = conf.contype
	local sid = conf.sid or 1
	-- assert(not user_agents[fd])
	-- local ip = string.sub(addr,1,string.find(addr,':'))
	-- if ip == '192.168.10.44' then 
	-- 	print("_____完成封ip不让连线__")
	-- 	return 
	-- end
	-- 这就是u_info
	self.userAgents[fd] = {
		fd = fd,
		contype = contype, --socket类型
		gate = gate,       
		queue = queue(),   --消息队列
		playerid = nil,    --
		addr = addr,       
		time = 0,          --计时器，长时间没反应删除socket
		sid = sid,         --消息验证id
	}
	skynet.call(gate, "lua", "forward", fd)

	-- skynet.fork(function()
	-- 	while(true) do 
	-- 	skynet.sleep(300)
	-- 		handler_login:test()
	-- 	end
	-- end)
		
end

--断线
function Command:disconnect(fd, msg, err)
	print("########disconnect#####")
	local userInfo = self.userAgents[fd]
	if userInfo then
		local playerid = userInfo.playerid
		if playerid then
			if msg == 'remote_login' then 
				print("######## remote_login #####")
				--发推送给之前的登录者	
				msgpack.send_message(fd, userInfo.contype, 'nt_remote_login', {
						id = 0,
						text = '帐号在异地登录',
					})		
			end			
			print('玩家离线>>>>>>>>>>>>>>>>',playerid,msg,err)
			self.userObj:disconnect(playerid, msg, err)
			local logoutObj = g_objects:getOne('Logout')
			logoutObj:add(playerid)
		end

	end
	if msg ~= 'logout' then 
		self.userAgents[fd] = nil	
	end
end


--通知玩家
function Command:notice( playerid, msg )
	if not playerid then
		local userList = self.userObj:getUserList()
		for k,v in pairs(userList) do
			self.userObj:notice(v.playerid, msg)
		end
	else
		self.userObj:notice(playerid, msg)
	end
end


--操作玩家信息
function Command:oper(playerid, cmd, ... )
	return self.msg:dispatchOper(playerid,cmd,...)
end

--异地登录，重连
function Command:redirect( fd, info, redirect, playerid )
	local old_userInfo = {
		client = info.fd,
		gate = info.gate,
		contype = info.contype,
		addr = info.addr,		
		sid = info.sid,		
	}
	self:start(old_userInfo)
	local userInfo = self.userAgents[fd]
	--_queue_request(userInfo, 'enter_gate_req', args)
	self.userObj:enterGate(userInfo, playerid, redirect)
end


return Command